package com.spa.interfaces.shop;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.spa.application.command.shop.ApplyRefundCommand;
import com.spa.application.command.shop.OrderOperTransferCommand;
import com.spa.application.dto.PageResult;
import com.spa.application.dto.shop.ApplyRefundRecordDTO;
import com.spa.application.dto.shop.OrderListDTO;
import com.spa.application.dto.shop.OrderTransferDto;
import com.spa.application.dto.shop.ShopOrderAllDTO;
import com.spa.application.query.shop.ApplyRefundListQuery;
import com.spa.application.query.shop.OrderListQuery;
import com.spa.application.query.shop.OrderQuery;
import com.spa.application.query.shop.OrderTransferCountQuery;
import com.spa.application.service.ShopApplicationService;
import com.spa.application.service.shop.ShopIndexService;
import com.spa.infrastructure.common.ResponseMessage;
import com.spa.interfaces.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/shop/index")
@Slf4j
public class ShopOrderController extends BaseController {

    @Autowired
    private ShopIndexService shopIndexService;

    @Autowired
    private ShopApplicationService shopApplicationService;

    @PostMapping("queryOrderList")
    public ResponseMessage<PageResult<OrderListDTO>> queryOrderList(@Validated @RequestBody OrderListQuery query) throws ParseException {
        return responseHelper.success(shopIndexService.queryOrderList(query));
    }

    @PostMapping("queryOrderDetail")
    public ResponseMessage<ShopOrderAllDTO> queryOrderDetail(@RequestBody OrderQuery query) {
        return responseHelper.success(shopIndexService.queryOrderDetail(query));
    }

    @PostMapping("/order/transfer/receiv")
    public ResponseMessage queryOrderDetail(@RequestBody OrderOperTransferCommand command) {
        Assert.isFalse( ObjectUtil.isNull(command.getOrderId())  , "缺少订单关键信息");
        Assert.isFalse( ObjectUtil.isNull(command.getMassagistId())  , "缺少被转单技师关键信息");
        shopApplicationService.orderTransferReceiv(command);
        return responseHelper.success();
    }

    @PostMapping("/order/transfer/count")
    public ResponseMessage<Integer> queryOrderTransferCount(@RequestBody OrderTransferCountQuery query) {
        return responseHelper.success(shopApplicationService.queryOrderTransferCount(query));
    }

    @PostMapping("/order/transfer/list")
    public ResponseMessage<List<OrderTransferDto>> queryOrderTransfer(@RequestBody OrderTransferCountQuery query) {
        Assert.isFalse( ObjectUtil.isNull(query.getStatus()) || (10 != query.getStatus() && 20 != query.getStatus() ) , "缺少转单关键信息");
        return responseHelper.success(shopApplicationService.queryOrderTransfer(query));
    }

    @PostMapping("/callUser")
    public ResponseMessage<String> callUser(@Validated @RequestBody OrderQuery query) throws Exception {
        return responseHelper.success(shopApplicationService.callUser(query.getOrderId()));
    }

    /**
     * 门店管理员对订单发起退款
     * @param command
     * @return
     */
    @PostMapping("applyRefund")
    public ResponseMessage applyRefund(@Validated @RequestBody ApplyRefundCommand command) {
        shopApplicationService.applyRefund(command);
        return responseHelper.success();
    }

    /**
     * 门店端分页查询申请退款记录
     * @param query
     * @return
     */
    @PostMapping("queryApplyRefundList")
    public ResponseMessage<PageResult<ApplyRefundRecordDTO>> queryApplyRefundList(@Validated @RequestBody ApplyRefundListQuery query) {
        return responseHelper.success(shopApplicationService.queryApplyRefundList(query));
    }
}
